'use strict';
app.controller('systemTypeCtrl', ['$scope', '$modal', '$log', '$state', 'utilService', 'toaster', 'systemTypeService', function ($scope, $modal, $log, $state, utilService, toaster, systemTypeService) {
    $scope.goodstypes = [];
    $scope.limit = 1;
    $scope.imgUrlsQueue = [];
    $scope.imgUrls = new Array();
    $scope.path = $scope.app.image.type;
    $scope.typeImgVO = {};
    //添加分类
    $scope.typeAddVO = {};
    //打开添加分类方框
    $scope.isOpen = false;
    $scope.typeEditVO = {};
    //
    $scope.goodstypeNames = [];

    $scope.typeBoo = true;



    //查询所有分类
    $scope.findAll = function () {
        $scope.app.loading = true;
        $scope.goodstypeNames = [];
        systemTypeService.findAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodstypes = result.data;
                    $scope.getAllName();
                    $scope.app.loading = false;
                    break;
                default:
                    $scope.app.loading = false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getAllName = function(){
        systemTypeService.getAllName($scope).then(function (result) {
            $scope.goodstypeNames = result.data;
        });
    }

    $scope.findAll();


    $scope.deconstruction  = function(aims,source){
        for (const key in source) {
            aims.push(source[key]);
            var chi = source[key].children;
            if( chi != null && chi.length>0){
                $scope.deconstruction(aims,chi);
            }
        }
    }

    $scope.addTypes = function () {
        if(!$scope.typeBoo){
            return;
        }
        $scope.typeBoo = false;
        systemTypeService.addTypes($scope).then(function (result) {
            $scope.typeBoo = true;
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findAll();
                    $scope.open();
                    $scope.typeAddVO = {};
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //添加子分类
    $scope.addChildType = function (parentId) {
        $scope.isOpen = true;
        roundTypes($scope.goodstypes);
        $scope.typeAddVO.parentId = parentId;
    }


    $scope.open = function () {
        roundTypes($scope.goodstypes);
        $scope.isOpen = !$scope.isOpen;
    }
    $scope.cancel = function () {
        $scope.isOpen = false;
    }


    //修改分类
    $scope.editShow = function (types) {
        roundTypes($scope.goodstypes);
        types.editVisiable = !types.editVisiable;
        $scope.typeEditVO.name = types.name;
    }
    $scope.cancelEdit = function (types) {
        types.editVisiable = false;
    }


    $scope.editTypes = function (type) {
        $scope.typeEditVO.id = type.id;
        if ($scope.typeEditVO.name == type.name) {
            $scope.cancelEdit(type)
            return;
        }

        systemTypeService.editTypes($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findAll();
                    $scope.cancelEdit(type);
                    $scope.typeEditVO = {};
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //类型图片
    $scope.init = function (type) {
        type.uploadVisiable = false;
        type.visiable = false;
        type.editVisiable = false;
    }

    var roundTypes = function (types) {
        for (var i = 0; i < types.length; i++) {
            types[i].uploadVisiable = false;
            types[i].editVisiable = false;
            if (types[i].children.length > 0) {
                roundTypes(types[i].children);
            }
        }
    }

    //删除分类
    $scope.deleteType = function (id, name) {
        roundTypes($scope.goodstypes);

        var param = {
            id: id,
            name: name
        };

        // query data.
        openModal("你是否要删除【" + name + "】分类以及其子分类？");

        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "删除分类",
                controller: "deleteTypeCtrl",
                message: msg,
                data: param
            });
        }
    }

    $scope.showPicBox = function (type) {
        $scope.imgUrlsQueue = [];
        roundTypes($scope.goodstypes);
        $scope.uploadState = true;
        $scope.typeImgVO.id = type.id;
        $scope.findbyid();
    }

    $scope.findbyid = function () {
        $scope.imgUrls = [];
        systemTypeService.findbyid($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    var data = result.data;
                    var imgUrl = data.imgUrl;
                    if (imgUrl == null || angular.isUndefined(imgUrl) || imgUrl == '') {
                        $scope.limit = 1;
                    } else {
                        $scope.limit = 0;
                        $scope.imgUrls.push(imgUrl);
                    }

                    break;
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancelUploadPic = function () {
        $scope.uploadState = false;
    }

    $scope.saveImg=function(){
        if($scope.imgUrlsQueue.length>0){
            $scope.typeImgVO.imgUrl=$scope.imgUrlsQueue[0].fileName;
        }

        systemTypeService.saveImg($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.reload('app.goodsTypes');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }

    $scope.delImg=function(){
        systemTypeService.delImg($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findbyid();
                    break;
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);

app.controller('deleteTypeCtrl', ['$scope', '$modalInstance', '$state', 'systemTypeService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, systemTypeService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.isDelModal = true;

        //确认删除
        $scope.ok = function () {
            systemTypeService.deleteType($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $modalInstance.dismiss('success');
                        $scope.findAll();
                        break;
                    default:
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);










